package com.jinxin.platform.cdfaceacs.utils;


import com.jinxin.platform.cdfaceacs.pojo.domains.AcsControlDeviceSubscribe;

import java.util.*;

/**
 * @Author: chenyinlong
 * @Description:
 * @Date: Create in 10:20 2020/5/9
 */
public class CollectionUtil {


    /**
     * 找出两个集合中不同的元素
     *
     * @param collmax
     * @param collmin
     * @return
     */
    public static List<String> getDifferentMac(List<AcsControlDeviceSubscribe> collmax, List<AcsControlDeviceSubscribe> collmin) {
        //使用LinkedList防止差异过大时,元素拷贝
        List<String> csReturn = new LinkedList();
        if (!collmax.get(0).getBccaAccount().equals(collmin.get(0).getBccaAccount())){
            return csReturn;
        }
        List<AcsControlDeviceSubscribe> max = collmax;
        List<AcsControlDeviceSubscribe> min = collmin;
        //先比较大小,这样会减少后续map的if判断次数
        if (collmax.size() < collmin.size()) {
            max = collmin;
            min = collmax;
        }
        //直接指定大小,防止再散列
        Map<String, Integer> map = new HashMap<>(max.size());
        for (AcsControlDeviceSubscribe object : max) {
            map.put(object.getDeviceMac(), 1);
        }
        for (AcsControlDeviceSubscribe object : min) {
            if (map.get(object.getDeviceMac()) == null) {
                csReturn.add(object.getDeviceMac());
            } else {
                map.put(object.getDeviceMac(), 2);
            }
        }
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                csReturn.add(entry.getKey());
            }
        }
        return csReturn;
    }

    /**
     * 找出两个集合中相同的元素
     *
     * @param collmax
     * @param collmin
     * @return
     */
    public static Collection getSame(Collection collmax, Collection collmin) {
        //使用LinkedList防止差异过大时,元素拷贝
        Collection csReturn = new LinkedList();
        Collection max = collmax;
        Collection min = collmin;
        //先比较大小,这样会减少后续map的if判断次数
        if (collmax.size() < collmin.size()) {
            max = collmin;
            min = collmax;
        }
        //直接指定大小,防止再散列
        Map<Object, Integer> map = new HashMap<>(max.size());
        for (Object object : max) {
            map.put(object, 1);
        }
        for (Object object : min) {
            if (map.get(object) != null) {
                csReturn.add(object);
            }
        }
        return csReturn;
    }
}
